import json
import logging
import traceback

import pymysql
from pymysql.constants import CLIENT

from config import BASE_PATH


def com_assert(status,des,resp):
    assert 200 == resp.status_code
    assert status == resp.json().get("status")

    assert des in resp.json().get("description")

def com_mysql(sql,db):
    conn=None
    cur=None
    try:
        conn=pymysql.connect(host="121.43.169.97", user="student", password="P2P_student_2023", database=db,client_flag=CLIENT.MULTI_STATEMENTS)
        cur=conn.cursor()
        cur.execute(sql)
        if sql.split(" ")[0].lower() =="select":
            res=cur.fetchall()
            print(f"查询到的结果是{res}")
            return res
        else:
            conn.commit()
            res=cur.rowcount
            print(f"受影响的行数为： {res}")
            return res
    except:
        print("---" * 30)
        traceback.print_exc()
        print("---" * 30)
    finally:
        if conn != None:
            conn.rollback()
            conn.close()
        elif cur != None:
            cur.close()


#提取数据
###   filename 文件名称（没有后缀）
###   test_data  测试的api名称
def get_data(filename,test_data):
    api_file=BASE_PATH+"/data/"+filename+".json"
    user_data_list=[]
    with open(file=api_file,encoding="utf-8",mode='r') as f:
        api_data=json.load(f)
        my_data=api_data.get(test_data)
        for data_dict in my_data:
            tup1=tuple(data_dict.values())
            user_data_list.append(tup1)
        print(user_data_list)
        logging.info(f"获取的用户数据：{user_data_list}")
    return user_data_list

if __name__ == '__main__':
    res = get_data("tender_data", "tender")
    print(res)
